using System;
using System.Collections.Generic;
using System.Text;

namespace ControlPoint.ControlAPI {
    public class ZWaveDevice : Device {
        protected ZWaveApi.Net.ZWaveNode _node;
        protected ZWaveApi.Net.CommandClasses.GenericCommandClass _commandClass;

        public ZWaveDevice() {
            throw new NotImplementedException();
        }


        public ZWaveDevice(ZWaveController controller, SettingsList settings) :
            base(controller, settings) {
            this.Settings.SettingChanged += OnSettingChanged;
            
            byte _nodeId;
            byte _instanceId;
            ZWaveApi.Net.CommandClasses.CommandClass _command;

            _nodeId = (byte)settings.GetSetting("nodeId").Value;

            if (this.Settings.ContainsSetting("nodeId")) {
                this._node = controller.Nodes[_nodeId];
            }

            _instanceId = (byte)settings.GetSetting("instanceId").Value;
            _command = (ZWaveApi.Net.CommandClasses.CommandClass)settings.GetSetting("command").Value;

            ZWaveApi.Net.Utilities.CommandIndex index = new ZWaveApi.Net.Utilities.CommandIndex(_nodeId, _command, _instanceId);

            MakeSureNodeHasCommand(index);
            _commandClass = this._node.CommandClasses[index];

            this._id = this._controller.ID + ":" + _nodeId + ":" + (byte)_command + ":" + _instanceId;

        }


        private void OnSettingChanged(Object sender, SettingChangedEventArgs e) {
			
        }


        private void MakeSureNodeHasCommand(ZWaveApi.Net.Utilities.CommandIndex index) {

            if (!this._node.CommandClasses.ContainsKey(index)) {
                string className = "ZWaveApi.Net.CommandClasses." + index.Command + ", ZWaveApi.Net";
                Type c = Type.GetType(className, true);



                ZWaveApi.Net.CommandClasses.GenericCommandClass gcc = (ZWaveApi.Net.CommandClasses.GenericCommandClass)Activator.CreateInstance(Type.GetType(className), new object[] { this._node, (byte)index.Instance });
                this._node.CommandClasses.Add(index, gcc);
            }

        }

        public byte NodeIdOld {
            get {
                return 0;
            }
            set {
                //0 = value;
            }
        }
        public override SettingsList Settings {
            get {
                return base.Settings;
            }
        }
    }
}